﻿Option Explicit On
Imports System.Xml

Public Class frmEditWeights

    Private weightsNames() As String
    Private weightsArray(,) As Double
    Private numWeightSets As Integer
    Private numWeights As Integer

    Private saved As Boolean
    Private t_weightName As String

    Public Event EditWeightsComplete()

    Public Sub EditWeightsSetup(ByVal numWeightsIn As Integer)
        ' Set up the grdWeights to have the appropriate number of columns, etc.
        With grdWeights

            .AutoRedraw = False
            .Column(0).Visible = False
            .BoldFixedCell = False

            numWeights = numWeightsIn
            .Cols = numWeights + 2 ' Add one for the name of the set and one for the hidden column.
            .Rows = 1
            numWeightSets = 0

            'Set the column headers
            .Cell(0, 1).Text = "Name"
            .Cell(0, 2).Text = "Str"
            .Cell(0, 3).Text = "AP"
            .Cell(0, 4).Text = "Crit"
            .Cell(0, 5).Text = "Haste"
            .Cell(0, 6).Text = "ArP"
            .Cell(0, 7).Text = "Agi"
            .Cell(0, 8).Text = "Armor"
            .Cell(0, 9).Text = "Hit"
            .Cell(0, 10).Text = "Spell Hit"
            .Cell(0, 11).Text = "Extra Hit"
            .Cell(0, 12).Text = "Exp"
            .Cell(0, 13).Text = "MH DPS"
            .Cell(0, 14).Text = "OH DPS"
            .Cell(0, 15).Text = "MH Speed"
            .Cell(0, 16).Text = "OH Speed"
            .Cell(0, 17).Text = "T7 2pc"
            .Cell(0, 18).Text = "T7 4pc"
            .Cell(0, 19).Text = "T8 2pc"
            .Cell(0, 20).Text = "T8 4pc"
            .Cell(0, 21).Text = "T9 2pc"
            .Cell(0, 22).Text = "T9 4pc"
            .Cell(0, 23).Text = "T10 2pc"
            .Cell(0, 24).Text = "T10 4pc"

            Dim loopvar As Integer
            .Column(1).Width = 130
            For loopvar = 2 To 9
                .Column(loopvar).Width = 40
            Next

            .Column(10).Width = 50
            .Column(11).Width = 50
            .Column(12).Width = 40
            .Column(13).Width = 50
            .Column(14).Width = 50
            For loopvar = 15 To 24
                .Column(loopvar).Width = 55
            Next

            .AutoRedraw = True
            .Refresh()

            setSaved(True)

        End With

    End Sub

    Public Sub SetWeights(ByVal setName As String, ByVal Index As Integer, ByVal value As Double)
        ' Just sets the value of the already dimensioned arrays
        Dim loopvar As Integer

        For loopvar = 0 To numWeightSets - 1
            If weightsNames(loopvar) = setName Then
                weightsArray(Index, loopvar) = value
            End If
        Next
    End Sub

    Public Sub AddWeightSet(ByVal setIndex As Integer, ByVal name As String)
        ' The first of these two functions.  Redims as necessary.
        Dim loopvar As Integer

        numWeightSets += 1
        ReDim Preserve weightsNames(0 To numWeightSets - 1)
        weightsNames(numWeightSets - 1) = name
        ReDim Preserve weightsArray(0 To numWeights - 1, 0 To numWeightSets - 1)
        For loopvar = 0 To numWeights - 1
            weightsArray(loopvar, numWeightSets - 1) = 0
        Next
    End Sub

    Public Sub DisplayWeights()
        ' Display the current weights in the flexgrid.
        Dim loopvar As Integer
        Dim loopvar2 As Integer

        grdWeights.AutoRedraw = False

        grdWeights.Rows = numWeightSets + 1
        For loopvar = 0 To numWeightSets - 1
            grdWeights.Cell(loopvar + 1, 1).Text = weightsNames(loopvar)
            For loopvar2 = 0 To numWeights - 1
                grdWeights.Cell(loopvar + 1, loopvar2 + 2).Text = weightsArray(loopvar2, loopvar).ToString("###0.000")
            Next
        Next

        grdWeights.AutoRedraw = True
        grdWeights.Refresh()
    End Sub

    Private Sub grdWeights_LeaveCell(ByVal Sender As Object, ByVal e As FlexCell.Grid.LeaveCellEventArgs) Handles grdWeights.LeaveCell
        ' Set the property.  t_gemName as the old gem name for matching purposes.
        Dim curWeightIndex As Integer
        Dim nameString As String

        'Default values.  If you leave a text cell
        ' it will stay blank.  If you leave a numeric cell
        ' it will be 0, color cell will be none. And a boolean cell will be no.
        If grdWeights.ActiveCell.Row = 0 Then Exit Sub

        If grdWeights.ActiveCell.Text = "" Then
            Select Case grdWeights.ActiveCell.Col
                Case 1
                    grdWeights.ActiveCell.Text = ""
                Case Else
                    grdWeights.ActiveCell.Text = "0"
            End Select
        End If

        If isValidValue(grdWeights.ActiveCell) = False Then
            If grdWeights.ActiveCell.Col <> 1 Then
                MessageBox.Show("Invalid Cell Value. Please enter a different value.", "DK Optimize", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
            Else
                MessageBox.Show("Duplicate name exists.  Please enter a different name.", "DK Optimize", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
            End If
            e.Cancel = True
        Else

            If e.Col = 1 Then
                nameString = t_weightName
            Else
                nameString = grdWeights.Cell(grdWeights.ActiveCell.Row, 1).Text
                ' Format the cell.
                grdWeights.ActiveCell.Text = Double.Parse(grdWeights.ActiveCell.Text).ToString("###0.000")
            End If

            curWeightIndex = ChangeProperty(nameString, grdWeights.ActiveCell.Col, grdWeights.ActiveCell.Row)

        End If
    End Sub

    Private Sub grdWeights_RowColChange(ByVal Sender As Object, ByVal e As FlexCell.Grid.RowColChangeEventArgs) Handles grdWeights.RowColChange
        ' Check name
        If grdWeights.ActiveCell.Col = 1 Then
            t_weightName = grdWeights.ActiveCell.Text
        End If
    End Sub

    Private Function isValidValue(ByVal cell As FlexCell.Cell) As Boolean
        ' Check for validity of the value in the cell passed in.  Use row / etc to check it.
        If cell.Row = 0 Then
            isValidValue = True
            Exit Function
        End If
        Select Case cell.Col
            Case 0 'startup / hidden column
                isValidValue = True
            Case 1 'name
                Dim loopvar As Integer
                For loopvar = 1 To grdWeights.Rows - 1
                    If cell.Text = grdWeights.Cell(loopvar, 1).Text And loopvar <> cell.Row Then
                        isValidValue = False
                        Exit Function
                    End If
                Next
                isValidValue = True
            Case Else
                If IsNumeric(cell.Text) = False Then
                    isValidValue = False
                Else
                    If Double.Parse(cell.Text) < 0 Or Double.Parse(cell.Text) > 10000 Then
                        isValidValue = False
                    Else
                        isValidValue = True
                    End If
                End If
        End Select
    End Function

    Private Function ChangeProperty(ByVal weightName As String, ByVal col As Integer, ByVal row As Integer) As Integer
        ' Searches for a gem by name in the gems() array in case of sorting, and then
        ' updates it.
        Dim weightIndex As Integer
        Dim loopvar As Integer
        Dim newProp As String

        For loopvar = 0 To UBound(weightsNames)
            If weightsNames(loopvar) = weightName Then
                weightIndex = loopvar
            End If
        Next

        newProp = grdWeights.Cell(row, col).Text

        Select Case col
            Case 1
                If weightsNames(weightIndex) <> newProp Then setSaved(False)
                weightsNames(weightIndex) = newProp
            Case 2 To grdWeights.Cols - 1
                If weightsArray(col - 2, weightIndex) <> newProp Then setSaved(False)
                weightsArray(col - 2, weightIndex) = Double.Parse(newProp)
        End Select

        ChangeProperty = weightIndex

    End Function

    Private Sub setSaved(ByVal state As Boolean)
        If state = True Then
            saved = True
            Me.Text = "DK Optimize - Edit Weights"
        Else
            saved = False
            Me.Text = "DK Optimize - Edit Weights*"
        End If
    End Sub

    Private Sub frmEditWeights_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        ' Checked saved, warn the user, cancel if they don't want to go.
        Dim msgresult As DialogResult

        grdWeights.Cell(grdWeights.ActiveCell.Row, 0).SetFocus()

        ' If there was an error, activecell will still have a value.
        If grdWeights.ActiveCell.Col <> 0 Then
            e.Cancel = True
            Exit Sub
        End If

        If saved = False Then
            msgresult = MessageBox.Show("Weights are not saved.  Would you like to save Weight information?", "DK Optimize", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
            Select Case msgresult
                Case Windows.Forms.DialogResult.Cancel
                    e.Cancel = True
                Case Windows.Forms.DialogResult.Yes
                    saveWeights()
                    setSaved(True)
                    e.Cancel = False
                    Me.Hide()
                    RaiseEvent EditWeightsComplete()
                Case Windows.Forms.DialogResult.No
                    e.Cancel = False
                    Me.Hide()
                    RaiseEvent EditWeightsComplete()
            End Select
        Else
            e.Cancel = False
            Me.Hide()
            RaiseEvent EditWeightsComplete()
        End If

    End Sub

    Private Sub saveWeights()
        ' does the actual work of saving.
        Dim loopvar As Integer
        Dim xmlWriter As New XmlTextWriter(Windows.Forms.Application.CommonAppDataPath & "\data_weights.xml", System.Text.Encoding.UTF8)
        Dim buildinfo As FileVersionInfo = FileVersionInfo.GetVersionInfo(Windows.Forms.Application.ExecutablePath)

        xmlWriter.WriteStartDocument()
        xmlWriter.WriteWhitespace(vbNewLine)
        xmlWriter.WriteStartElement("weightsets")
        xmlWriter.WriteAttributeString("version", buildinfo.ProductVersion)
        For loopvar = 0 To UBound(weightsNames)
            xmlWriter.WriteWhitespace(vbNewLine)
            xmlWriter.WriteWhitespace(vbTab)
            xmlWriter.WriteStartElement("set")
            xmlWriter.WriteAttributeString("name", weightsNames(loopvar))
            xmlWriter.WriteWhitespace(vbNewLine)
            xmlWriter.WriteWhitespace(vbTab & vbTab)
            xmlWriter.WriteElementString("str", weightsArray(0, loopvar).ToString(System.Globalization.CultureInfo.InvariantCulture))
            xmlWriter.WriteWhitespace(vbNewLine)
            xmlWriter.WriteWhitespace(vbTab & vbTab)
            xmlWriter.WriteElementString("ap", weightsArray(1, loopvar).ToString(System.Globalization.CultureInfo.InvariantCulture))
            xmlWriter.WriteWhitespace(vbNewLine)
            xmlWriter.WriteWhitespace(vbTab & vbTab)
            xmlWriter.WriteElementString("crit", weightsArray(2, loopvar).ToString(System.Globalization.CultureInfo.InvariantCulture))
            xmlWriter.WriteWhitespace(vbNewLine)
            xmlWriter.WriteWhitespace(vbTab & vbTab)
            xmlWriter.WriteElementString("haste", weightsArray(3, loopvar).ToString(System.Globalization.CultureInfo.InvariantCulture))
            xmlWriter.WriteWhitespace(vbNewLine)
            xmlWriter.WriteWhitespace(vbTab & vbTab)
            xmlWriter.WriteElementString("arp", weightsArray(4, loopvar).ToString(System.Globalization.CultureInfo.InvariantCulture))
            xmlWriter.WriteWhitespace(vbNewLine)
            xmlWriter.WriteWhitespace(vbTab & vbTab)
            xmlWriter.WriteElementString("agi", weightsArray(5, loopvar).ToString(System.Globalization.CultureInfo.InvariantCulture))
            xmlWriter.WriteWhitespace(vbNewLine)
            xmlWriter.WriteWhitespace(vbTab & vbTab)
            xmlWriter.WriteElementString("armor", weightsArray(6, loopvar).ToString(System.Globalization.CultureInfo.InvariantCulture))
            xmlWriter.WriteWhitespace(vbNewLine)
            xmlWriter.WriteWhitespace(vbTab & vbTab)
            xmlWriter.WriteElementString("hit", weightsArray(7, loopvar).ToString(System.Globalization.CultureInfo.InvariantCulture))
            xmlWriter.WriteWhitespace(vbNewLine)
            xmlWriter.WriteWhitespace(vbTab & vbTab)
            xmlWriter.WriteElementString("sphit", weightsArray(8, loopvar).ToString(System.Globalization.CultureInfo.InvariantCulture))
            xmlWriter.WriteWhitespace(vbNewLine)
            xmlWriter.WriteWhitespace(vbTab & vbTab)
            xmlWriter.WriteElementString("exhit", weightsArray(9, loopvar).ToString(System.Globalization.CultureInfo.InvariantCulture))
            xmlWriter.WriteWhitespace(vbNewLine)
            xmlWriter.WriteWhitespace(vbTab & vbTab)
            xmlWriter.WriteElementString("exp", weightsArray(10, loopvar).ToString(System.Globalization.CultureInfo.InvariantCulture))
            xmlWriter.WriteWhitespace(vbNewLine)
            xmlWriter.WriteWhitespace(vbTab & vbTab)
            xmlWriter.WriteElementString("mhdps", weightsArray(11, loopvar).ToString(System.Globalization.CultureInfo.InvariantCulture))
            xmlWriter.WriteWhitespace(vbNewLine)
            xmlWriter.WriteWhitespace(vbTab & vbTab)
            xmlWriter.WriteElementString("ohdps", weightsArray(12, loopvar).ToString(System.Globalization.CultureInfo.InvariantCulture))
            xmlWriter.WriteWhitespace(vbNewLine)
            xmlWriter.WriteWhitespace(vbTab & vbTab)
            xmlWriter.WriteElementString("mhspeed", weightsArray(13, loopvar).ToString(System.Globalization.CultureInfo.InvariantCulture))
            xmlWriter.WriteWhitespace(vbNewLine)
            xmlWriter.WriteWhitespace(vbTab & vbTab)
            xmlWriter.WriteElementString("ohspeed", weightsArray(14, loopvar).ToString(System.Globalization.CultureInfo.InvariantCulture))
            xmlWriter.WriteWhitespace(vbNewLine)
            xmlWriter.WriteWhitespace(vbTab & vbTab)
            xmlWriter.WriteElementString("t7_2", weightsArray(15, loopvar).ToString(System.Globalization.CultureInfo.InvariantCulture))
            xmlWriter.WriteWhitespace(vbNewLine)
            xmlWriter.WriteWhitespace(vbTab & vbTab)
            xmlWriter.WriteElementString("t7_4", weightsArray(16, loopvar).ToString(System.Globalization.CultureInfo.InvariantCulture))
            xmlWriter.WriteWhitespace(vbNewLine)
            xmlWriter.WriteWhitespace(vbTab & vbTab)
            xmlWriter.WriteElementString("t8_2", weightsArray(17, loopvar).ToString(System.Globalization.CultureInfo.InvariantCulture))
            xmlWriter.WriteWhitespace(vbNewLine)
            xmlWriter.WriteWhitespace(vbTab & vbTab)
            xmlWriter.WriteElementString("t8_4", weightsArray(18, loopvar).ToString(System.Globalization.CultureInfo.InvariantCulture))
            xmlWriter.WriteWhitespace(vbNewLine)
            xmlWriter.WriteWhitespace(vbTab & vbTab)
            xmlWriter.WriteElementString("t9_2", weightsArray(19, loopvar).ToString(System.Globalization.CultureInfo.InvariantCulture))
            xmlWriter.WriteWhitespace(vbNewLine)
            xmlWriter.WriteWhitespace(vbTab & vbTab)
            xmlWriter.WriteElementString("t9_4", weightsArray(20, loopvar).ToString(System.Globalization.CultureInfo.InvariantCulture))
            xmlWriter.WriteWhitespace(vbNewLine)
            xmlWriter.WriteWhitespace(vbTab & vbTab)
            xmlWriter.WriteElementString("t10_2", weightsArray(21, loopvar).ToString(System.Globalization.CultureInfo.InvariantCulture))
            xmlWriter.WriteWhitespace(vbNewLine)
            xmlWriter.WriteWhitespace(vbTab & vbTab)
            xmlWriter.WriteElementString("t10_4", weightsArray(22, loopvar).ToString(System.Globalization.CultureInfo.InvariantCulture))
            xmlWriter.WriteWhitespace(vbNewLine)
            xmlWriter.WriteWhitespace(vbTab)
            xmlWriter.WriteEndElement()
        Next
        xmlWriter.WriteWhitespace(vbNewLine)
        xmlWriter.WriteEndElement()
        xmlWriter.WriteEndDocument()
        xmlWriter.Close()
    End Sub

    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        ' Writes back out to the XML file data.xml  Just replaces the weights part with
        ' the current contents of our weightsarrays
        Dim msgResult As DialogResult

        ' move the focus off screen since we left hte control.
        ' This also calls leave_cell, which updates our variables etc.
        grdWeights.Cell(grdWeights.ActiveCell.Row, 0).SetFocus()
        If grdWeights.ActiveCell.Col <> 0 Then Exit Sub

        msgResult = MessageBox.Show("Are you sure you want to save all weights?", "DK Optimize", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
        If msgResult = Windows.Forms.DialogResult.No Then Exit Sub

        saveWeights()
        setSaved(True)
    End Sub

    Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        ' Adds a row to the grid and takes a name from the user for the new weights.  
        ' Sets other values to zero
        Dim inputResult As String
        Dim loopvar As Integer

        ' move the focus off screen since we left the control.
        ' This also calls leave_cell, which updates our variables etc.
        grdWeights.Cell(grdWeights.ActiveCell.Row, 0).SetFocus()
        If grdWeights.ActiveCell.Col <> 0 Then Exit Sub

        inputResult = InputBox("Please enter a name for the new Weights:", "DK Optimize")

        If inputResult <> "" Then
            For loopvar = 0 To numWeightSets - 1
                If weightsNames(loopvar) = inputResult Then
                    MessageBox.Show("Set name already exists.  Please select a different name.", "DK Optimize", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1)
                    Exit Sub
                End If
            Next
            numWeightSets += 1
            ReDim Preserve weightsNames(0 To numWeightSets - 1)
            ReDim Preserve weightsArray(0 To numWeights - 1, 0 To numWeightSets - 1)
            weightsNames(numWeightSets - 1) = inputResult
            grdWeights.Rows += 1
            grdWeights.Cell(grdWeights.Rows - 1, 1).Text = inputResult
            For loopvar = 0 To numWeights - 1
                weightsArray(loopvar, numWeightSets - 1) = 0
                grdWeights.Cell(grdWeights.Rows - 1, loopvar + 2).Text = "0.000"
            Next
            setSaved(False)
        End If
    End Sub

    Private Sub btnDel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDel.Click
        ' Verifies, and then deletes if necessary
        ' weights from the group and more importantly
        ' the array.
        Dim msgResult As DialogResult

        ' move the focus off screen since we left hte control.
        ' This also calls leave_cell, which updates our variables etc.
        grdWeights.Cell(grdWeights.ActiveCell.Row, 0).SetFocus()
        If grdWeights.ActiveCell.Col <> 0 Then Exit Sub

        If grdWeights.ActiveCell.Row = 0 Then Exit Sub

        msgResult = MessageBox.Show("Are you sure you want to delete """ & grdWeights.Cell(grdWeights.ActiveCell.Row, 1).Text & """?", "DK Optimize", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2)
        If msgResult = Windows.Forms.DialogResult.No Then Exit Sub

        Dim loopvar As Integer
        Dim loopvar2 As Integer
        Dim curWeightIndex As Integer

        For loopvar = 0 To UBound(weightsNames)
            If weightsNames(loopvar) = grdWeights.Cell(grdWeights.ActiveCell.Row, 1).Text Then
                curWeightIndex = loopvar
            End If
        Next

        grdWeights.RemoveItem(grdWeights.ActiveCell.Row)
        For loopvar = curWeightIndex To UBound(weightsNames) - 1
            weightsNames(loopvar) = weightsNames(loopvar + 1)
            For loopvar2 = 0 To numWeights - 1
                weightsArray(loopvar2, loopvar) = weightsArray(loopvar2, loopvar + 1)
            Next
        Next
        numWeightSets -= 1
        ReDim Preserve weightsNames(0 To numWeightSets - 1)
        ReDim Preserve weightsArray(0 To numWeights - 1, 0 To numWeightSets - 1)
        setSaved(False)

    End Sub
End Class